﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace liwuyou.sprider
{
    public partial class frmMainForm : Form
    {
        public frmMainForm()
        {
            InitializeComponent();
        }

        #region 检查子窗口是否存在
        private Boolean CheckMdiFromExit(string formName)
        {
            foreach (Form childrenForm in this.MdiChildren)
            {
                if (childrenForm.Name == formName)
                {
                    childrenForm.Activate();
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 各个窗口打开程序
        // 打开库存同步窗口
        private void MI_StockSyn_Click(object sender, EventArgs e)
        {
            if (!CheckMdiFromExit("StockSyn"))
            {
                Form stockSynForm = new frmStockSyn();
                stockSynForm.MdiParent = this;
                stockSynForm.Show();
            }
        }

        // 打开发货同步窗口
        private void MI_TradeSyn_Click(object sender, EventArgs e)
        {
            if (!CheckMdiFromExit("TradeSyn"))
            {
                Form stockMdiChild = new frmTradeSyn();
                stockMdiChild.MdiParent = this;
                stockMdiChild.Show();
            }
        }

        // 打开订单抓取窗口
        private void MI_OrderGet_Click(object sender, EventArgs e)
        {
            if (!CheckMdiFromExit("OrderGet"))
            {
                Form stockMdiChild = new frmOrderGet();
                stockMdiChild.MdiParent = this;
                stockMdiChild.Show();
            }
        }

        // 打开数据库设置窗口
        private void MI_DBSetting_Click(object sender, EventArgs e)
        {
            Form setDBForm = new frmSetDB();
            setDBForm.ShowDialog();
        }

        // 打开任务设置窗口
        private void MI_TaskSetting_Click(object sender, EventArgs e)
        {
            Form setTaskForm = new frmSetTask();
            setTaskForm.ShowDialog();
        }

        #endregion

        #region 私有方法　处理窗体的　显示　隐藏　关闭(退出)
        /// <summary>
        /// 退出主程序
        /// </summary>
        private void ExitMainForm()
        {
            if (MessageBox.Show("您确定要退出管家助手吗？", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        /// <summary>
        /// 隐藏主窗口
        /// </summary>
        private void HideMainForm()
        {
            this.Hide();
        }

        /// <summary>
        /// 显示主窗口
        /// </summary>
        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }
        #endregion

        #region 右键菜单处理，显示　隐藏　退出
        /// <summary>
        /// 显示主窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiShow_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }

        /// <summary>
        /// 隐藏主窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiHide_Click(object sender, EventArgs e)
        {
            HideMainForm();
        }

        /// <summary>
        /// 显示帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiHelp_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            ExitMainForm();
        }
        #endregion

        #region 双击托盘上图标时，显示窗体
        /// <summary>
        /// 双击托盘 显示或者隐藏窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                HideMainForm();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                ShowMainForm();
            }
        }
        #endregion

        #region 点最小化按钮时，最小化到托盘
        /// <summary>
        /// 点最小化按钮时，最小化到托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }
        }
        #endregion

        #region 窗体关闭时最小化到托盘
        /// <summary>
        /// 窗体关闭时最小化到托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //TODO: 去掉发行版此注释
            //e.Cancel = true;

            //HideMainForm();
        }
        #endregion
    }
}
